package org.bigjava.user.web.controller;


import org.bigjava.bean.Posts;
import org.bigjava.common.Constant;
import org.bigjava.user.service.impl.PostsServiceImpl;
import org.bigjava.user.util.AESUtil;
import org.bigjava.user.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;


@Controller
@RequestMapping("/posts/")
public class PostsController {
    @Autowired
    PostsServiceImpl postsService;
    AESUtil aesUtil = new AESUtil();
    DateUtil date = new DateUtil();

    //创建帖子
    @RequestMapping("createposts")
    public void createposts(Posts posts, HttpSession session) {
//        posts.setPostTitle("test");
//          posts.setPostImage("");
//        posts.setPostValue("test value ");
//        posts.setPostPhoto("aaaa");
//        System.out.println(dateFormat.format(date.getTime()));
//        posts.setPostDate(date.getTime());
//        posts.setPostReplys(10);
//        posts.setPostState(0);
//        posts.setPostLike(0);
//        posts.setPostUnlike(0);
//        posts.setPostHots(0);
//        posts.setPostConllect(0);
        int userid = (int) session.getAttribute(Constant.userid);   //获取用户id
        posts.setPostUser(userid);      //设置创建帖子的用户id
        posts.setTopicId((Long) session.getAttribute(Constant.sessionTopicId)); //设置创建帖子的话题id
        postsService.insertPosts(posts);
    }

    //查看帖子详情信息
    @RequestMapping("queryPosts")
    public ModelAndView queryPosts(String postsName, int topicId, HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();
        if (postsService.queryPostsDetails(postsName, topicId) == null) {
            modelAndView.setViewName("postsDelete");
        } else {
            Posts posts = postsService.queryPostsDetails(postsName, topicId);
            session.getServletContext().setAttribute(Constant.sessionPostsId, posts.getPostId());
            session.getServletContext().setAttribute("postsDetails", posts);
            modelAndView.setViewName("postsDetails");
        }
        return modelAndView;
    }


    //用户查看自己的所有帖子
    @RequestMapping("queryAllMyPosts")
    public ModelAndView queryAllMyPosts(HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();
        long id = (long) session.getAttribute(Constant.userid);
        List<Posts> posts = postsService.queryAllMyPosts(id);
        modelAndView.addObject("AllMyPosts", posts);
        modelAndView.setViewName("index");
        return modelAndView;
    }

    //用户删除帖子（修改帖子state为2）
    @RequestMapping("deletePosts")
    public ModelAndView deletePosts(int postsId, int topicId) {
        ModelAndView modelAndView = new ModelAndView();
        postsService.deletePosts(postsId, topicId);
        modelAndView.setViewName("myposts");
        return modelAndView;
    }

    //对帖子进行评论
    @RequestMapping("commentPosts")
    public void commentPosts(HttpSession session) {


    }

}
